const host = require('../../config').host
const app = getApp()

Page({
    data: {
        //判断小程序的API，回调，参数，组件等是否在当前版本可用。
        canIUse: wx.canIUse("button.open-type.getUserInfo")
        // canIUse: wx.canIUse("button.open-type.getUserInfo")
    },
    onLoad: function () {
        var that = this;
        
        
    },
    bindGetUserInfo: function (e) {
      // console.log(e.detail)
     wx.login({
      success: function(res) {
        // console.log(res)
         app.globalData.code = res.code
        }
      });

     try{
        app.globalData.token = wx.getStorageSync("token")

      } catch(e){}

        if (e.detail.userInfo) {
            //用户按了允许授权按钮
            var that = this;
            var userInfo = e.detail
            // var code = ""
            // 查看是否授权
          wx.getSetting({
            success: function (res) {
              if (res.authSetting['scope.userInfo']) {
                
                  wx.getUserInfo({
                      success: function (res) {
                          //插入登录的用户的相关信息到数据库
                          wx.request({
                              url: host + 'api/tourist/weixin_login',
                              data: {
                                  encryptedData: userInfo.encryptedData,
                                  iv: userInfo.iv,
                                  code: app.globalData.code,
                                  token: app.globalData.token
                              },
                              header: {
                                  'content-type': 'application/json'
                              },
                              success: function (res) {
                                  //从数据库获取用户信息
                                  // that.queryUsreInfo();
                                  // console.log(res.data.data[0]);
                                  if(res.data.sta == 1){
                                    app.globalData.userInfo = e.detail.userInfo;
                                    app.globalData.userType = res.data.data.huiyuan[0].type
                                    wx.switchTab({
                                      url: `./user`,
                                      success: function (e) {
                                        var page = getCurrentPages().pop();
                                        // console.log(page);
                                        if (page == undefined || page == null) return;
                                        page.onLoad();
                                      }
                                    });
                                  }else{
                                    wx.showModal({
                                        title:'警告',
                                        content:'登录出错',
                                        showCancel:false,
                                        confirmText:'返回授权',
                                        success:function(res){
                                            if (res.confirm) {
                                                console.log('用户点击了“返回授权”')
                                            } 
                                        }
                                    })
                                  }
                              }
                          });
                          //授权成功后，跳转进入小程序首页
                      }
                  });
              }
            }
        })
            
            
        } else {
            //用户按了拒绝按钮
            wx.showModal({
                title:'警告',
                content:'您点击了拒绝授权，可能无法正常使用部分功能!!!',
                showCancel:false,
                confirmText:'返回授权',
                success:function(res){
                    if (res.confirm) {
                        console.log('用户点击了“返回授权”')
                    } 
                }
            })
        }
    },
    //获取用户信息接口
    queryUsreInfo: function () {
        wx.request({
            url: getApp().globalData.urlPath + 'hstc_interface/queryByOpenid',
            data: {
                openid: getApp().globalData.openid
            },
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                console.log(res.data);
                getApp().globalData.userInfo = res.data;
            }
        });
    },
    
})